package com.justnow.deadlock;

/**
 * @author justnow
 * Created on 2020-10-03
 * Description
 */
public class DeadLockDemo01 {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock1) {
                System.out.println(Thread.currentThread().getName() + " get lock1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " try to get lock2");
                synchronized (lock2) {
                    System.out.println(Thread.currentThread().getName() + " have got lock2");
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock2) {
                System.out.println(Thread.currentThread().getName() + " get lock2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " try to get lock1");
                synchronized (lock1) {
                    System.out.println(Thread.currentThread().getName() + " have got lock1");
                }
            }
        }).start();
    }
}
